<?php

namespace catchAdmin\model;

use catcher\exceptions\AlertErrorException;

class Need extends ModelApp
{

    protected $append = [
        'company_type_name',
        'project_jia_ji_name',
        'publish_time_show',
        'status_time_show',
        'status_name',
    ];

    protected $type = [
        'sys_label_id' => 'array',
    ];


    ########################## Attr start ######################


    /**
     * left_day
     */
    public function getLeftDayAttr()
    {
        $left_day = ceil((strtotime($this->getOrigin('need_ent_date')) - time()) / 3600 / 24);

        if ($left_day < 0) {
            $left_day = 0;
        }

        return $left_day;
    }


    /**
     * company_type_name_list
     */
    public function getCompanyTypeNameList()
    {
        return [
            1 => '居间人',
            2 => '工程',
            3 => '物资',
            4 => '设计',
            5 => '监理',
            6 => '劳务',
            7 => '物资贸易商',
            8 => '协会',
        ];
    }


    /**
     * company_type_name
     */
    public function getCompanyTypeNameAttr()
    {
        return $this->getCompanyTypeNameList()[$this->getOrigin('company_type')] ?? '';
    }


    public function getStageLlist()
    {
        return [
            [
                'name'    => '待审核',
                'id'      => '1',
                'disable' => true,
                'badge'   => [
                    'value' => 0,
                ],
            ],
            [
                'name'    => '招幕中',
                'id'      => '2',
                'disable' => true,
                'badge'   => [
                    'value' => 0,
                ],
            ],
            [
                'name'    => '被拒绝',
                'id'      => '3',
                'disable' => true,
                'badge'   => [
                    'value' => 0,
                ],
            ],
            [
                'name'    => '已完成',
                'id'      => '4',
                'disable' => true,
                'badge'   => [
                    'value' => 0,
                ],
            ],
        ];
    }


    /**
     * project_jia_ji_list
     */
    public function getProjectJiaJiList()
    {
        return [
            1 => '否',
            2 => '是',
        ];
    }


    /**
     * project_jia_ji_name
     */
    public function getProjectJiaJiNameAttr()
    {
        return $this->getProjectJiaJiList()[$this->getOrigin('project_jia_ji')] ?? '';
    }


    /**
     * publish_time_show
     */
    public function getPublishTimeShowAttr()
    {
        if (empty($this->getOrigin('publish_time'))) {
            return "——";
        }

        return date('Y-m-d H:i:s', $this->getOrigin('publish_time'));
    }


    /**
     * status_time_show
     */
    public function getStatusTimeShowAttr()
    {
        if (empty($this->getOrigin('status_time'))) {
            return "——";
        }

        return date('Y-m-d H:i:s', $this->getOrigin('status_time'));
    }


    /**
     * need_city_code
     */
    public function getNeedCityCodeAttr()
    {
        return [
            substr($this->getOrigin('need_city_code'), 0, 2) . "0000",
            substr($this->getOrigin('need_city_code'), 0, 4) . "00",
            $this->getOrigin('need_city_code'),
        ];
    }


    /**
     * status_name_list
     */
    public function getStatusNameList()
    {
        return [
            1 => '审核中',
            2 => '通过',
            3 => '未通过',
        ];
    }


    /**
     * status_name
     */
    public function getStatusNameAttr()
    {
        return $this->getStatusNameList()[$this->getOrigin('status')] ?? '';
    }




    ########################## Attr end ######################
    ###################### Search start #################

    public function searchCompanyTypeAttr($query, $value, $data)
    {
        $query->where('company_type', $value);
    }


    public function searchNeedTitleAttr($query, $value, $data)
    {
        $query->where('need_title', 'like', '%' . $value . '%');
    }


    public function searchProjectJiaJiAttr($query, $value, $data)
    {
        $query->where('project_jia_ji', 'like', '%' . $value . '%');
    }


    public function searchNeedCityCodeAttr($query, $value, $data)
    {
        if (is_array($value)) {
            $value = end($value);
        }

        if (substr($value, 2) == '0000') {
            $value = substr($value, 0, 2);
        } elseif (substr($value, 4, 2) == '00') {
            $value = substr($value, 0, 4);
        }

        $query->where('need_city_code', "like", $value . '%');
    }


    public function searchStageAttr($query, $value, $data)
    {
        $query->where('stage', $value);
    }


    ###################### Search start #################

    public function saveDataApp($param)
    {
        if (!$this->getCompanyIs()) {
            throw new AlertErrorException('只有公司审核通过，才能发布需求');
        }

        if (!empty($param['id'])) {
            $need_msg = $this
                ->where([
                    'id'             => $param['id'],
                    'wechat_user_id' => $this->getLoginId(),
                ])
                ->find();
            if (empty($need_msg)) {
                throw new AlertErrorException();
            }

            if ($need_msg['status'] == 2) {
                throw new AlertErrorException('已通过，不能再编辑');
            }
        }

        $msg_arr = [
            'id'                => $param['id'] ?? 0,
            'company_type'      => $param['company_type'] ?? 1,
            'need_title'        => $param['need_title'] ?? '',
            'need_type'         => $param['need_type'] ?? '',
            'need_amount'       => $param['need_amount'] ?? '',
            'need_yao_qiu'      => $param['need_yao_qiu'] ?? '',
            'need_time'         => $param['need_time'] ?? '',
            'need_pay_type'     => $param['need_pay_type'] ?? '',
            'need_city_address' => $param['need_city_address'] ?? '',
            'company_yao_qiu'   => $param['company_yao_qiu'] ?? '',
            'need_ent_date'     => $param['need_ent_date'] ?? '',

            'goods_name'    => $param['goods_name'] ?? '',
            'goods_attr'    => $param['goods_attr'] ?? '',
            'goods_num'     => $param['goods_num'] ?? '',
            'goods_num_img' => $param['goods_num_img'] ?? '',

            'wechat_user_id' => $this->getLoginId(),
            'status'         => 1,
            'status_note'    => '',
            'status_time'    => 0,
            'stage'          => 1,
        ];

        // 需求类型，需求名称
        {
            if (!empty($need_msg)) {
                $msg_arr['company_type'] = $need_msg['company_type'];
            }

            if ($msg_arr['company_type'] == 3) {
                $msg_arr['need_title'] = $msg_arr['goods_name'];
            }
        }


        // 认证公司信息
        {
            $user_company_msg = (new UserCompany())
                ->where('wechat_user_id', $msg_arr['wechat_user_id'])
                ->where('company_status', 3)
                ->find();
            $msg_arr['company_name'] = $user_company_msg['company_name'] ?? '';
            $msg_arr['linkman_realname'] = $user_company_msg['linkman_realname'] ?? '';
            $msg_arr['linkman_duty'] = $user_company_msg['linkman_duty'] ?? '';
            $msg_arr['linkman_mobile'] = $user_company_msg['linkman_mobile'] ?? '';
        }

        // 城市名
        $msg_arr['need_city_name'] = "";
        if (!empty($param['need_city_code']) && is_array($param['need_city_code'])) {
            $name_arr = [];
            foreach ($param['need_city_code'] as $key => $value) {
                $name_arr[] = (new Area())
                    ->where('code', $value)
                    ->value('name') ?? '';
            }

            $msg_arr['need_city_code'] = end($param['need_city_code']);
            $msg_arr['need_city_name'] = implode('', $name_arr);
        }

        // 标签
        $msg_arr['sys_label_id'] = $param['sys_label_id'] ?? [];
        $msg_arr['sys_label_name'] = (new SysLabel())->getLabelNameStr($msg_arr['sys_label_id']);

        return parent::saveDataCC($msg_arr);
    }


    /**
     * 详情
     */
    public function readData($param)
    {
        return $this
            ->where('id', $param['id'])
            ->find();
    }


}
